package org.dc.activity.schedule;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import org.dc.R;
import org.dc.entity.Shift;
import org.dc.entity.WorkDay;
import org.dc.service.PersistenceManager;
import org.dc.utlity.UIUtility;

import java.util.*;

/**
 * @user: Vladimir Chernishov
 * @date: 2/28/12
 * @time: 5:29 PM
 */
public class Schedule extends Activity {

    public static final String REQUESTED_DATE = "DATE";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.schedule);
    }

    private void refreshTable() {
        TableLayout table = (TableLayout) findViewById(R.id.scheduleTable);

        ArrayList<TextView> columns = new ArrayList<TextView>();
        TableRow tableColumns = (TableRow) table.getChildAt(0);
        for (int i = 0; i < tableColumns.getChildCount(); i++) {
            columns.add((TextView) tableColumns.getChildAt(i));
        }

        ArrayList<TextView> days = new ArrayList<TextView>();
        for (int i = 1; i < table.getChildCount(); i++) {
            TableRow tableRow = (TableRow) table.getChildAt(i);
            for (int ii = 0; ii < tableRow.getChildCount(); ii++) {
                days.add((TextView) tableRow.getChildAt(ii));
            }
        }


        int pixels = (getWindowManager().getDefaultDisplay().getWidth() - 20);
        UIUtility.updateTableWidth(table, pixels);

        Calendar calendar = (Calendar) getIntent().getSerializableExtra(REQUESTED_DATE);
        if (calendar == null) {
            calendar = GregorianCalendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
        }

        Map<Integer,Shift> shifts = PersistenceManager.getShiftDao(this).getShiftsMap();


        List<WorkDay> workDays = PersistenceManager.getWorkDayDao(this).getWorkDaysForMonth((Calendar)calendar.clone());
        fillDebugTable(workDays, (TableLayout) findViewById(R.id.debugScheduleTable));
        Iterator<WorkDay> workDaysIt = workDays.iterator();
        WorkDay workDay;

        if(workDaysIt.hasNext()){
            workDay = workDaysIt.next();
        } else {
            workDay = null;
        }


        calendar.set(Calendar.DAY_OF_MONTH, 1);
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        if (day == Calendar.SUNDAY) {
            day = 6;
        } else {
            day -= 2;
        }

        int i = 0;
        for (; i < day; i++) {
            days.get(i).setBackgroundResource(R.color.LIGHT_GRAY);
        }

        for (int max = calendar.getActualMaximum(Calendar.DAY_OF_MONTH), ii = 1; ii <= max; ii++, i++) {
            TextView cell = days.get(i);
            if(workDay != null && workDay.getCalendar().get(Calendar.DAY_OF_MONTH) == ii){
                Shift shift = shifts.get(workDay.getShiftId());
                cell.setText(ii + "\n " + (shift == null ? "day off " : shift.getName()));
                if(workDaysIt.hasNext()){
                    workDay = workDaysIt.next();
                }else {
                    workDaysIt = null;
                }
            } else {
                cell.setText(String.valueOf(ii));
            }
        }
        for (; i < days.size(); i++) {
            days.get(i).setBackgroundResource(R.color.LIGHT_GRAY);
        }
    }

    private void fillDebugTable(List<WorkDay> workDays, TableLayout table) {
        table.removeAllViews();
        for (WorkDay workDay : workDays) {
            TableRow row = new TableRow(this);
            TextView date = new TextView(this);
            date.setText(workDay.getDate().toString());
            row.addView(date);
            TextView schedule = new TextView(this);
            schedule.setText(String.valueOf(workDay.getShiftId()));
            row.addView(schedule);
            table.addView(row);
        }
    }

    public void createSchedule(View v) {
        Intent i = new Intent(this, FillSchedule.class);
        startActivity(i);
    }

    public void clearSchedule(View v) {
        PersistenceManager.getWorkDayDao(this).clearSchedule();
        this.finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        refreshTable();
    }
}
